package com.bipowernode.jdbc;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * @author desire
 */
public class test01 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        String driver = bundle.getString("jdbc.driver");
        String url = bundle.getString("jdbc.url");
        String username = bundle.getString("jdbc.username");
        String password = bundle.getString("jdbc.password");
        try {
            //1.注册驱动
            Class.forName(driver);
            //2.获取连接
            conn = DriverManager.getConnection(url,username,password);
            //3.数据库操作对象  //4.执行sql
            String sql = "select ename,job,deptno FROM emp";
            pstmt = conn.prepareStatement(sql);
            //5.处理结果集
            rs = pstmt.executeQuery();
            while (rs.next()) {
                String ename = rs.getString("ename");
                String job = rs.getString("job");
                String deptno = rs.getString("deptno");
                System.out.println(ename + "," + job + "," + deptno);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(pstmt != null){
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
